package net.crowdloud.shared.engine;

import java.util.ArrayList;
import java.util.List;

import net.crowdloud.client.GlobalVariables;
import net.crowdloud.shared.entities.Groups;
import net.crowdloud.shared.entities.User;

public class CrowdLoudGroupsEngine {

	public static List<User> getUsersGroup(List<User> allUsers,
			String groupTarget) {

		List<User> usersList = new ArrayList<User>();

		for (User user : allUsers) {

			if (Groups.GROUP_NONE.equals(groupTarget)) {

				if (user.getGroups() == null) {

					usersList.add(user);

				} else {

					String[] groupsArray = GlobalVariables.groups.split(",");

					boolean userGroupNone = true;

					for (String group : groupsArray) {

						if (user.getGroups().contains(group.trim())) {

							userGroupNone = false;

							break;
						}
					}
					if (userGroupNone) {

						usersList.add(user);
					}
				}
			} else {

				if (user.getGroups() != null
						&& user.getGroups().contains(groupTarget.trim())) {

					usersList.add(user);
				}
			}
		}

		return usersList;
	}
}
